# Dictionary 字典

# 1.访问字典
login_info = {'username': 'admin', 'password': '12345', 'login_type': 'QQ'}
print(login_info['username'])
print(login_info['password'])
print(login_info['login_type'])

# 2.使用函数访问元素
# 2.1 keys(),values(),items()
login_info = {'username': 'admin', 'password': '12345', 'login_type': 'QQ'}
print(login_info.keys())
print(login_info.values())
print(login_info.items())

for k in login_info.keys():
    print(k)
print("-----------")
for k, v in login_info.items():
    print(k, v)

# 3.修改或添加字典元素，使用dict[key]=value或dict.update()
login_info = {'username': 'admin', 'password': '12345', 'login_type': 'QQ'}
# 3.1 dict[key]=value
login_info['login_type'] = 'Wechat'
print(login_info)
# 3.2 update()
login_info.update({'login_type': 'zhifubao'})
print(login_info)

login_info.update({'remember_pwd': True})
print(login_info)

# 4.删除，使用del语句，如果key不存在，产生keyError的错误
login_info = {'username': 'admin', 'password': '12345', 'login_type': 'QQ'}
del login_info['login_type']
print(login_info)

# 5.清除所有字典元素，使用dict.clear()
login_info = {'username': 'admin', 'password': '12345', 'login_type': 'QQ'}
login_info.clear()
print(login_info)
